import * as API from '../../../api/pedestal';
import * as util from '../../../utils/util';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataList: [],
    isShowModal:false,
    id: null,
    form: {
    pedestalTypeName: null,
    display:false
  }
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (options) {
    this.getList()
  },

  modalOpen: function (e) {
    let id = e.target.dataset.id
    API.detailType(id).then(res => {
      this.setData({
        form: res.data||[],
        id:id
      })
    })
    this.setData({
      isShowModal: true
    })
  },
  hideMarker: function () {
    this.setData({
      isShowModal: false
    })
  },
  addModal(){
    let form =  {
      pedestalTypeName: null,
      display:false
    }
    this.setData({
      form: form,
      isShowModal: true,
      id: null
    })
  },
  deleteModal(e){
    let id= e.target.dataset.id
    let that = this
    wx.showModal({
      title: '提示',
      content: '确定要删除吗？',
      success: function (sm) {
        if (sm.confirm) {
            // 用户点击了确定 可以调用删除方法了
            that.deleteAction(id)
          }
        }
      })
    
  },
  deleteAction(id){
    API.deleteType(id).then(res => {
      wx.showToast({
        icon: 'success',
        title: '删除成功',
        duration: 1500
      });
      this.getList()
    })
  },
  // cancelOrderFormSubmit(){
  //   this.setData({
  //     showOrderCancelDialog: false
  //   })
  // },
  getList(){
    wx.showLoading ({});
    API.getProductType().then(res => {
      wx.hideLoading()
      this.setData({
        dataList: res.data||[]
      })
    }, err => {
      wx.hideLoading();
    })
  },
  formSubmit(e) {
    if (this.validForm(e)) {
      let postbody = {
        pedestalTypeName: e.detail.value.pedestalTypeName,
        display: e.detail.value.display
      }
      wx.showLoading ({});
      if(this.data.id) {
        API.editType(this.data.id, postbody).then(res => {
          wx.hideLoading()
          wx.showToast({
            icon: 'success',
            title: '修改成功',
            duration: 1500
          });
          this.getList()
          this.hideMarker()
        }, err => {
          wx.hideLoading();
          wx.showToast({
            icon: 'none',
            title: err || '修改失败'
          });
        })
      } else {
        API.addType(postbody).then(res => {
          wx.hideLoading()
          wx.showToast({
            icon: 'success',
            title: '添加成功',
            duration: 1500
          });
        this.getList()
          this.hideMarker()
        }, err => {
          wx.hideLoading();
          wx.showToast({
            icon: 'none',
            title: err || '添加失败'
          });
        })
      }
    }
  },
  //提交验证
 validForm(e) {
  var pedestalTypeName = e.detail.value.pedestalTypeName;

  if (!pedestalTypeName || pedestalTypeName < 1) {
    wx.showToast({
      title: '分类名称不能为空',
      icon: 'none'
    });
    return false;
  }
  return true;
},
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },


  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})